package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysConfig;
import com.linq.cool.system.dto.SysConfigQueryDTO;
import com.linq.cool.system.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/25 10:47
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("config")
public class SysConfigController extends BaseController {
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize(hasPerm = "sys:config:list")
    @GetMapping("list")
    public Result<?> list(SysConfigQueryDTO dto) {
        log.info("SysConfigQueryDTO: {}", dto);
        IPage<SysConfig> page = sysConfigService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "{configId}")
    public Result<?> getInfo(@PathVariable Long configId) {
        return Result.ok(sysConfigService.getById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "configKey/{configKey}")
    public Result<?> getConfigKey(@PathVariable String configKey) {
        return Result.ok(sysConfigService.getByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @Log(moduleTitle = "参数管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "sys:config:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysConfig config) {
        if (sysConfigService.checkKeyUnique(config)) {
            return Result.fail("新增参数'" + config.getName() + "'失败，参数键名已存在");
        }
        config.setCreator(SecurityUtils.getUserId());
        return this.toResult(sysConfigService.saveConfig(config));
    }

    /**
     * 修改参数配置
     */
    @Log(moduleTitle = "参数管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:config:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysConfig config) {
        if (sysConfigService.checkKeyUnique(config)) {
            return Result.fail("修改参数'" + config.getName() + "'失败，参数键名已存在");
        }
        config.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(sysConfigService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @Log(moduleTitle = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "sys:config:remove")
    @DeleteMapping("/{configIds}")
    public Result<?> remove(@PathVariable List<Long> configIds) {
        return this.toResult(sysConfigService.deleteByIds(configIds));
    }

    /**
     * 清空缓存
     */
    @Log(moduleTitle = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "sys:config:remove")
    @DeleteMapping("/clearCache")
    public Result<?> clearCache() {
        sysConfigService.clearCache();
        return Result.ok();
    }

}
